if (window.plus) {
    merchantApplyReady();
} else {
    document.addEventListener('plusready', merchantApplyReady, false);
}
$(function() {
    if (AGENT_CLIENT.WEB) {
        merchantApplyReady();
    }
})

function merchantApplyReady() {
    var user = {}, currentLoginUser = {};
    var applyPage = function() {
        this.init();
    };
    applyPage.prototype = {
        init: function() {
          this.bindEvent();
        },
        bindEvent: function() {
            //返回
            $("#back").on("click", function() {
                if(AGENT_CLIENT.APP){
                	plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");
                }else{
                	back();
                }
            });
            // 要做商探
            $("#merchantSelect").on("click", function() {
		        	user = getLocalStorage(STORAGE_MANAGER.user);
		        	if(!user){
		        		redirect("../account/login.html?backurl=merchant|apply.html");
		        		return;
		        	}
		          $.ajax({
		              headers: {
		                  "token": user.token
		              },
		              type: "GET",
		              url: CONFIG_MANAGER.RequstUrl + "v1/member/" + user.id,
		              beforeSend: function(){ $.showWaiting(); }
		          }).done(function (data) {
		              setLocalStorage(STORAGE_MANAGER.currentmember, data);
		            	currentLoginUser = getLocalStorage(STORAGE_MANAGER.currentmember);
		            	if(!currentLoginUser.phone){
										$.alert("请先绑定手机号", function(e) {
											redirect("../account/setphone.html?backurl=merchant|apply.html");
										});
		            	}else if(currentLoginUser.is_agent){
		            		redirect("select.html");
		            	}else{
		            		$("#is_agent_sign").fadeIn(200);
		            	}		              
		          }).always(function(){
		          	$.closeWaiting();
		          });
            });
            // 关闭签约层
            $("#btnclose").on("click", function() {
                $("#is_agent_sign").fadeOut(200);
            });
            // 同意
            $("#checkbox-1-1").on("change", function() {
                var isActive = $(this).prop("checked");
                if (isActive) {
                    $("#btnsign").addClass("button-active");
                } else {
                    $("#btnsign").removeClass("button-active");
                }
            });

            // 签约
            $("#btnsign").on("click", function() {
                $.ajax({
                  type: "POST",
                  url: CONFIG_MANAGER.RequstUrl + "v1/member/sign/agent/" + user.id,
                  async: true,
                  headers: { token: user.token },
                  beforeSend: function() {
                      $("#is_agent_sign").hide();
                      $.showWaiting();
                  }
                }).done(function(data) {
                    $.toast("签约成功");
                    var currentmember = getLocalStorage(STORAGE_MANAGER.currentmember);
                    currentmember.is_agent = true;
                    setLocalStorage(STORAGE_MANAGER.currentmember, currentmember);
                    setTimeout(function() {
                        redirect("../merchant/select.html", false, "pop-in", function() {
                            plus.webview.currentWebview().close();
                        });
                    }, 1500);
                }).fail(function(data) {
                    var infor = JSON.parse(data.responseText);
                    $.alert(infor.Message, function() {
                        if (infor && infor.Message && infor.Message.indexOf('已经签约') > -1) {
                            var currentmember = getLocalStorage(STORAGE_MANAGER.currentmember);
                            currentmember.is_agent = true;
                            setLocalStorage(STORAGE_MANAGER.currentmember, currentmember);
                            redirect("../merchant/select.html", false, "pop-in", function() {
                                plus.webview.currentWebview().close();
                            });
                        }
                    });
                }).always(function() {
                    $.closeWaiting();
                });
            });
        },
    };
    var _applyPage = new applyPage();
}